package cn.org.donald.framework.config;

/**
 * @author ： Donald
 * @date ： 2020/10/22 21:30
 * @description：
 */

import cn.org.donald.framework.property.SwaggerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


/**
 * @author ： Donald
 * @date ： 2020/10/22 20:26
 * @description：
 */
@Configuration
//@EnableSwaggerBootstrapUI
public class SwaggerConfig implements WebMvcConfigurer {

    @Autowired
    private SwaggerProperties swaggerProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    private static final String splitor = ";";

    /*api接口扫描路径*/
    //private static final  String SWAGGER_SCAN_BASE_PACKAGE_0 = "com.donald.controller";

    //private static final  String SWAGGER_SCAN_BASE_PACKAGE_1 = "com.donald.framework.authentication.controller";

    /*文档版本*/
    private static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(
                        RequestHandlerSelectors.withClassAnnotation(RestController.class)

                        /*RequestHandlerSelectors.basePackage(
                        SWAGGER_SCAN_BASE_PACKAGE_0
                                + splitor
                                + SWAGGER_SCAN_BASE_PACKAGE_1
                        )*/
                )
                .paths(PathSelectors.any())
                .build()
                .enable(swaggerProperties.getEnable());
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Donald")/*文档标题*/
                .description("Donald Framework接口文档")
                .version(VERSION)
                .build();
    }

}
